package connect4client.commands;

import messages.InvitationReplyMessage;
import connect4client.core.GameManager;
import connect4client.core.Invitation;

/**
 * The InviteReply command.
 * 
 * This command will handle Invite messages.
 */
public class InvitationReplyCommand extends Command {

	public InvitationReplyCommand(InvitationReplyMessage inviteReplyMessage){
		super(inviteReplyMessage);
	}

	@Override
	public void execute() {
		InvitationReplyMessage message = (InvitationReplyMessage) this.message; 		
		GameManager gameManager = controller.getGameManager();
		
		synchronized(gameManager) {
			int replyId = message.getReplyId();	
			Invitation invitation = gameManager.getInvitationByID(replyId);
			String gameName = invitation.getGameName();

			if (invitation.getSender().getLogin().equals(controller.getUser().getLogin())) {
				if (message.isAccepted())
					guiMediator.invitationAccepted(invitation.getSender(), gameName);
				else
					guiMediator.invitationDenied(invitation.getSender(), gameName);
			}
		}
	}
}
